package com.wuzheng.wisgo.sanitation.cloud.controller.facility;

import com.wuzheng.wisgo.sanitation.cloud.model.common.param.DeleteParam;
import com.wuzheng.wisgo.sanitation.cloud.model.facility.param.FacilityDetailParam;
import com.wuzheng.wisgo.sanitation.cloud.model.facility.param.FacilityListParam;
import com.wuzheng.wisgo.sanitation.cloud.model.facility.pojo.FacilityDTO;
import com.wuzheng.wisgo.sanitation.cloud.repository.entity.person.User;
import com.wuzheng.wisgo.sanitation.cloud.service.facility.FacilityService;
import com.wuzheng.wisgo.sanitation.framework.base.annotation.Authorization;
import com.wuzheng.wisgo.sanitation.framework.base.annotation.CurrentUser;
import com.wuzheng.wisgo.sanitation.framework.base.controller.BaseController;
import com.wuzheng.wisgo.sanitation.framework.core.constant.SysRespEnum;
import com.wuzheng.wisgo.sanitation.framework.core.protocol.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author：hoipang
 * @date：Created in 2019-12-03
 * @description：环卫设施信息控制器
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("/facility")
@Api(value = "FacilityCtrl", tags = "环卫设施信息接口")
public class FacilityCtrl extends BaseController {

    @Autowired
    private FacilityService facilityService;

    /**
     * 查询环卫设施特定信息列表(支持分页)
     */
    @PostMapping(value = "/list")
    @ApiOperation(value = "查询环卫设施特定信息列表(支持分页)")
    public Result<List<FacilityDTO>> list(@RequestBody FacilityListParam facilityListParam) {
        return this.success(SysRespEnum.SUCCESS.getMessage(), facilityService.listFacilityDTOs(facilityListParam));
    }

    /**
     * 删除环卫设施信息
     */
    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除环卫设施信息(支持批量)")
    public Result<String> delete(@RequestBody DeleteParam deleteParam) {
        facilityService.removeFacilities(deleteParam.getIds());
        return this.success(SysRespEnum.SUCCESS.getMessage());
    }

    /**
     * 获取环卫设施详情
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取环卫设施详情")
    public Result<FacilityDTO> get(@PathVariable("id") String id) {
        return this.success(SysRespEnum.SUCCESS.getMessage(), facilityService.selectFacilityDTO(id));
    }

    /**
     * 保存环卫设施信息
     */
    @Authorization
    @PostMapping(value = "/save")
    @ApiOperation(value = "保存环卫设施信息")
    public Result<FacilityDTO> insertOrUpdate(@ApiIgnore @CurrentUser User user,
                                              @RequestBody FacilityDetailParam facilityDetailParam,
                                              BindingResult bindingResult) throws BindException {
        if (bindingResult.hasErrors()) {
            throw new BindException(bindingResult);
        }
        return this.success(SysRespEnum.SUCCESS.getMessage(), facilityService.insertOrUpdate(user, facilityDetailParam));
    }
}

